package io.nutz.nutzsite.module.sys.services;

import io.nutz.nutzsite.common.base.Service;
import io.nutz.nutzsite.common.socket.WkWebSocket;
import io.nutz.nutzsite.common.socket.WkWsHandler;
import io.nutz.nutzsite.module.sys.models.MsgUser;

import java.util.List;

import javax.websocket.Session;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Each;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemove;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;
import org.nutz.plugins.wkcache.annotation.CacheResult;

/**
 * 推送消息用户 服务层实现
 * 
 * @author lrw
 * @date 2020-03-27
 */
@IocBean(args = {"refer:dao"})
@CacheDefaults(cacheName = "sys_msg_user")
public class MsgUserService extends Service<MsgUser> {
	
	private static final Log log = Logs.get();
	public MsgUserService(Dao dao) {
		super(dao);
	}
	
    /**
     * 获取未读消息数量
     *
     * @param loginname
     * @return
     */
    @CacheResult(cacheKey = "${args[0]}_getUnreadNum")
    public int getUnreadNum(String loginname) {
        int size = this.count(Cnd.where("delFlag", "=", false).and("loginname", "=", loginname)
                .and("status", "=", 0));
        return size;
    }

    /**
     * 获取未读消息列表
     *
     * @param loginname
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @CacheResult(cacheKey = "${args[0]}_getUnreadList")
    public List<MsgUser> getUnreadList(String loginname, int pageNumber, int pageSize) {
        return this.query(Cnd.where("delFlag", "=", false).and("loginname", "=", loginname)
                .desc("opAt"), "msg", Cnd.orderBy().desc("sendAt"), new Pager().setPageNumber(pageNumber).setPageSize(pageSize));
    }

    @CacheRemove(cacheKey = "${args[0]}_*")
    //可以通过el表达式加 * 通配符来批量删除一批缓存
    public void deleteCache(String loginname) {

    }

    @CacheRemoveAll
    public void clearCache() {

    }
    
}
